package com.hssoftvn.android.apps.callmessmoneycontroller.modules;


import java.io.StringReader;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.content.Intent;
import android.util.Log;

import com.hssoftvn.android.apps.callmessmoneycontroller.enums.SConstanst;
import com.hssoftvn.android.apps.callmessmoneycontroller.util.GiXMLParser;
import com.hssoftvn.android.apps.callmessmoneycontroller.util.SUtils;

public class OpenCellID {
	  String mcc;  //Mobile Country Code
	  String mnc;  //mobile network code
	  String cellid; //Cell ID
	  String lac;  //Location Area Code
	   
	  Boolean error;
	  String strURLSent;
	  String GetOpenCellID_fullresult;
	   
	  String latitude;
	  String longitude;
	  
	  private SAXParserFactory spf;
		private SAXParser sp;
		private XMLReader xr;
		private GiXMLParser mXMLParser;
	  
	  public Boolean isError(){
	   return error;
	  }
	   
	  public void setMcc(String value){
	   mcc = value;
	  }
	   
	  public void setMnc(String value){
	   mnc = value;
	  }
	   
	  public void setCallID(int value){
	   cellid = String.valueOf(value);
	  }
	   
	  public void setCallLac(int value){
	   lac = String.valueOf(value);
	  }
	   
	  public String getLocation(){
	   return(latitude + " : " + longitude);
	  }
	   
	  public void groupURLSent(){
	   strURLSent =
	    "http://www.opencellid.org/cell/get?mcc=" + mcc
	    +"&mnc=" + mnc
	    +"&cellid=" + cellid
	    +"&lac=" + lac
	    +"&fmt=txt"
		+"&key="+ API_KEY;
	  }
	   
	  public String getstrURLSent(){
	   return strURLSent;
	  }
	   
	  public String getGetOpenCellID_fullresult(){
	   return GetOpenCellID_fullresult;
	  }
	  //Check here for more info: http://wiki.opencellid.org/wiki/API
	  //Account: huynhhoangson@gmail.com pass so.......
	  //key to use 110ed8ef-bfa3-4d54-9c7d-7df700be7fba
	  //MCC, MNC : http://mcclist.com/mobile-network-codes-country-codes.asp
	  public final String API_KEY="110ed8ef-bfa3-4d54-9c7d-7df700be7fba";
	  public void GetOpenCellID() {
		
			try {
				Thread thread = new Thread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						try {
							groupURLSent();
						    HttpClient client = new DefaultHttpClient();
						    HttpGet request = new HttpGet(strURLSent);
						    Log.d(SUtils.TAG, "URL=" + strURLSent);
						    HttpResponse response;
							response = client.execute(request);
							Log.d(SUtils.TAG, "URL-- response === " + response);
							GetOpenCellID_fullresult = EntityUtils.toString(response.getEntity()); 
							splitResult();
							//if (!error){
								readResponse(GetOpenCellID_fullresult);
							//}
							Intent i = new Intent(SConstanst.INTENT_ACTION_NOTIFY_OPENCELLID_RESULT);
							i.putExtra(SConstanst.EXTRA_OPENCELLID_BOOL_HASERROR, error);
							if (!error){								
								i.putExtra(SConstanst.EXTRA_OPENCELLID_INT_LATITUDE, latitude);
								i.putExtra(SConstanst.EXTRA_OPENCELLID_INT_LONGITUDE, longitude);
							}
							SUtils.showDialogMessage("Test", "data from XMLParser = " + mXMLParser.getResponse().getErrorInfo().toString());
							SUtils.getAppContext().sendBroadcast(i);
						}catch (Exception eee){
							eee.printStackTrace();  
						}
					}
				});
				thread.start();
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	  }
	   
	  private void splitResult(){
		  Log.d(SUtils.TAG, GetOpenCellID_fullresult);
		   if(GetOpenCellID_fullresult.contains("err info")){
		    error = true;
		   }else{
		    error = false;
		    String[] tResult = GetOpenCellID_fullresult.split(",");
		    latitude = tResult[0];
		    longitude = tResult[1];
		   }    
	  }
	  public OpenCellID(){
		  try {
				/** Handling XML */
				spf = SAXParserFactory.newInstance();
				sp = spf.newSAXParser();
				xr = sp.getXMLReader();
				
				mXMLParser = new GiXMLParser();
				xr.setContentHandler(mXMLParser);
				
			} catch (Exception e) {
				Log.e(SUtils.TAG,"XML Server Parsing Exception = " + e);
			}
	  }
	  private void parseXML (InputSource isrc)
		{
			try {
				xr.parse(isrc);
			} catch (Exception e) {
				Log.e(SUtils.TAG,"parseXML Parsing Exception = " + e);
			}
		}
	  
	  public void readResponse (String text)
		{
			try {
				/** Handling XML */
				// convert String into InputStream
				parseXML(new InputSource(new StringReader(text)));
			} catch (Exception e) {
				Log.e(SUtils.TAG,"parseXMLFileOffline Parsing Exception = " + e);
			}
		}
	 }
